#!usr/bin/python
# -*- coding:utf-8 -*-

#统一修改文件夹下所有文件的格式为utf-8 Bom
#目前支持文件后缀cpp, h, qml

import os, codecs, chardet

#要修改的文件夹目录
# rootDir = 'F:\projects\svnHMI\SOC\EJANUS2\hmi'
rootDir = 'F:\projects\controls'
# rootDir = 'F:\\projects\\python\\toUtf8Bom\\55'

def bianli(path):
    list = os.listdir(path)
    for i in range(0, len(list)):
        tempPath = os.path.join(path, list[i])
        if os.path.isdir(tempPath):
            bianli(tempPath)
        else:
            print(tempPath)
            if tempPath.endswith('.h') or tempPath.endswith('.cpp') or tempPath.endswith('.qml'):
                # data = ''
                with open(tempPath, 'r') as f:
                    data = f.read()

                changeData = data.decode('utf_8_sig')

                with codecs.open(tempPath, 'w', 'utf_8_sig') as f:
                    f.write(changeData)

if __name__ == '__main__':
    bianli(rootDir)